package org.example.core.Service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.example.core.Mapper.UserFundMapper;
import org.example.core.Mapper.UserFundRecordMapper;
import org.example.core.Service.UserFundRecordService;
import org.example.core.pojo.User;
import org.example.core.pojo.UserFund;
import org.example.core.pojo.UserFundRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* <p>
* sg_user_fund_record Service 接口实现
* </p>
*
* @author luoxu
* @since 2024-08-27 11:11:49
*/
@Service
@Transactional
@Slf4j
public class UserFundRecordServiceImpl extends ServiceImpl<UserFundRecordMapper, UserFundRecord> implements UserFundRecordService {
    @Autowired
    UserFundRecordMapper userFundRecordMapper;
    @Autowired
    UserFundMapper userFundMapper;

    @Override
    public void pointsFreeze(Long userId, Integer money) {
        //判断用户是否存在
        UserFund User = getUser(userId);
        //判断积分是否足够
        Long temp=User.getScore()-money;
        if(temp>=0){
            User.setScore(temp);
            User.setFreezeScore(User.getFreezeScore()+money);
            userFundMapper.updateById(User);
            //进行增加积分记录与冻结积分记录,1代表积分冻结，0代表积分划扣
            addLog(userId,1,money);
            addLog(userId,0,-money);
        }else {
            log.info("用户积分不足");
            throw new RuntimeException("用户积分不足");
        }
    }


    @Override
    public void freezeReturn(Long userId, Integer money) {
         UserFund User = getUser(userId);
         if(User.getFreezeScore()>=money){
             User.setFreezeScore(User.getFreezeScore()-money);
             User.setScore(User.getScore()+money);
             userFundMapper.updateById(User);
             addLog(userId,1,-money);
             addLog(userId,0,money);
         }

    }

    @Override
    public void pointDeduction(Long userId, Integer money) {
        //判断用户是否存在
        UserFund User = getUser(userId);
        UserFund allUser= getUser(0L);
        Long temp=User.getFreezeScore()-money;
        if(temp>=0){
            User.setFreezeScore(temp);
            addLog(userId,1,-money);
            userFundMapper.updateById(User);
            allUser.setScore(allUser.getScore()+money);
            addLog(allUser.getId(),0,money);
            userFundMapper.updateById(allUser);
        }else {
            log.info("用户冻结积分不足");
            throw new RuntimeException("用户冻结积分不足");
        }
    }

    @Override
    public void directDeduction(Long userId, Integer money) {
        UserFund userFund = getUser(userId);
        if(userFund.getScore()>=money){
            userFund.setScore(userFund.getScore()-money);
            userFundMapper.updateById(userFund);
            UserFund allFund = getUser(0L);
            allFund.setScore(allFund.getScore()+money);
            userFundMapper.updateById(allFund);
            addLog(userFund.getId(),0,-money);
            addLog(allFund.getId(),0,money);
        }else{
            throw new RuntimeException("账户积分不足");
        }

    }
    public void addLog(Long id, Integer type,Integer money){
        UserFundRecord log=new UserFundRecord();
        log.setFundId(id);
        log.setFundType(type);
        log.setMoney(money);
        userFundRecordMapper.insert(log);
    }
    public UserFund getUser(Long userId){
        UserFund User = userFundMapper.selectOne(Wrappers.<UserFund>lambdaQuery().eq(UserFund::getUserId, userId));
        if (User == null) {
            User=new UserFund();
            User.setUserId(userId);
            User.setFreezeScore(0L);
            User.setScore(50L);
            User.setVersion(0L);
            userFundMapper.insert(User);
        }
        return User;
    }
}